package com.ywfStudy.ywfliving.commodity.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.ywfStudy.common.valid.SaveGroup;
import com.ywfStudy.common.valid.UpdateGroup;
import com.ywfStudy.common.valid.UpdateIsShowGroup;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ywfStudy.ywfliving.commodity.entity.BrandEntity;
import com.ywfStudy.ywfliving.commodity.service.BrandService;
import com.ywfStudy.common.utils.PageUtils;
import com.ywfStudy.common.utils.R;



/**
 * 家居品牌
 *
 * @author ywf
 * @email 993530928@qq.com
 * @date 2023-04-06 15:50:39
 */
@RestController
@RequestMapping("commodity/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:brand:info")
    public R info(@PathVariable("id") Long id){
		BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * @Validated: 注解的作用就是启用BrandEntity字段的校验
     * 如果没有写该注解，那么在BrandEntity的校验规则不会生效
     * BindingResult result: SpringBoot 会将校验的错误放入到result
     * 程序员可以通过result将校验的错误取出 进行处理
     *
     * @Validated({SaveGroup.class}): 表示调用save时，进行参数校验，使用的是SaveGroup的校验规则
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:brand:save")
    public R save(@Validated({SaveGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*/) {

        brandService.save(brand);
        return R.ok();
        //if (result.hasErrors()) {
        //    //创建一个map，用于接收校验错误
        //    Map<String, String> map = new HashMap<>();
        //    result.getFieldErrors().forEach((item) -> {
        //        //得到field
        //        String field = item.getField();
        //        //得到校验错误的信息
        //        String defaultMessage = item.getDefaultMessage();
        //        //放入到map
        //        map.put(field, defaultMessage);
        //    });
        //    return R.error(400, "品牌表单数据不合法").put("data", map);
        //}else {
        //
        //    brandService.save(brand);
        //
        //    return R.ok();
        //}
    }

    /**
     * 修改
     * @Validated({SaveGroup.class}): 表示调用save时，进行参数校验，使用的是SaveGroup的校验规则
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    @RequestMapping("/update/isshow")
    //@RequiresPermissions("commodity:brand:update")
    public R UpdateIsShowGroup(@Validated({UpdateIsShowGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:brand:delete")
    public R delete(@RequestBody Long[] ids){
		brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
